package com.itheima.controller;


import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.dto.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.qiniu.util.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Value("${health.path}")
    private String path;

    // 按条件分页查询套餐
    @PostMapping("/findPage")
    public PageResult selectfindPage(@RequestBody QueryPageBean pageBean) {
        PageResult pageResult = setmealService.selectfindPage(pageBean);
        return pageResult;
    }

    // 上传照片
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) throws IOException {
        String s = UUID.randomUUID().toString() + ".jpg";
        String jpgPath = path + s;
        imgFile.transferTo(new File(jpgPath));
        return Result.success("上传成功",jpgPath);
    }

    /*
     * 添加套餐
     * */
    @PostMapping
    public Result addSetmeal(@RequestBody Setmeal setmeal
            , Integer[] checkgroupIds) {
        //List<CheckGroup> checkGroups = setmealService.queryCheckgroup(checkgroupIds);
        //setmeal.setCheckGroups(checkGroups);
        setmealService.addSetmeal(setmeal, checkgroupIds);
        return Result.success("新增套餐成功");
    }

    /*
     * 根据id查询套餐
     * */
    @GetMapping("/{id}")
    public Result querySetmealById(@PathVariable Integer id) {
        Setmeal setmeal = setmealService.selectSetmealById(id);
        return Result.success(setmeal, "查询套餐数据成功");
    }


    /*
     * 删除套餐
     * */
    @DeleteMapping("/{id}")
    public Result delectById(@PathVariable Integer id) {
        setmealService.deleteById(id);
        return Result.success("删除成功");
    }

    /*
     * 根据套餐id查询对应的检查组id集合
     * 返回检查组的id数组
     * */
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result queryCheckgroupIdsBySetmealId(Integer id) {
        //Integer i = Integer.parseInt(id);
        List<Integer> ids = setmealService.queryCheckgroupIdsBySetmealId(id);
        //String s = ids.toString();
        return Result.success(ids,"查询套餐成功");
    }

    /*
     * 编辑套餐
     * */
    @PutMapping
    public Result updateSetmeal(@RequestParam List<Integer> checkgroupIds, @RequestBody Setmeal setmeal) {
        //首先根据setmeal_id清空setmeal下面的数据
        setmealService.updateSetmeal(checkgroupIds,setmeal);
        return Result.success("编辑成功");
    }


}
